<?php
namespace Home\Controller;
use Common\Controller\UserController;
class UserrightController extends UserController
{
   public function address(){
   	$id =session('user_id');
   	$AddressModel = D('address');
   	if (IS_POST) {            
      $data=array(
	    		'name'=>I('post.name'),
	    		'user_id' => session('user_id'),
	    		'province'=>I('post.province'),
	    		'city'=> I('post.city'),
	    		'county'=> I('post.county'),
	    		'email'=> I('post.email'),
	    		'mail'=> I('post.mail'),
	    		'phone'=>I('post.phone'),
	    		'telphone'=> I('post.telphone'),
	    		'RadioGroup'=> I('post.RadioGroup'),	    		
    		);
      if ($AddressModel->create($data)) {
   	  	  $AddressModel->add();
        }else{
    		$this->error($AddressModel->getError());
    	}          
   	 }
  	  $result = $AddressModel->where("user_id={$id}")->select(); 
  	  foreach ($result as $key => $value) { 
	  	  	$region = M('region');
	  	  	$p = $region->where("id=".$value['province'])->find();
	  	  	$c = $region->where("id=".$value['city'])->find();
	  	  	$co = $region->where("id=".$value['county'])->find();
	  	  	$result[$key]['address'] = $p['name'].$c['name'].$co['name'];
	  }
  	  $this->result =$result;
      $this->assign('address',U('default_address'));     		
      $this->assign('addaddress',U('Addaddress')); 
      $this->display();
    }

    public function default_address(){
      $address_id = I('post.address_id');
      $addressModel = M('address');
      if ($address_id>0) {
          $addressModel->status=1;
          $row = $addressModel->where("id={$address_id}")->save();
          if ($row) {
            $data['status']='ok';
            $this->ajaxReturn($data);
          }else{
            $data['status']='error';
            $data['info'] = $addressModel->geterror();
            $this->ajaxReturn($data);
          }
      }
    }

    public function Addaddress(){
      $regionModel = M('region');
    	$this->states  =$regionModel->where('pid=1')->select();
      $this->areas = $regionModel->where('pid=2')->select();
    	$this->locals = $regionModel->where('pid=33')->select();
    	$this->region = U('region');
    	$addressModel=D('address');    	
    	$this->address=U('address');
    	$this->id = session('user_id');   	
      $this->display();

    }

    public function region(){
    	if(IS_AJAX){
    		$pid=I('post.pid',0,'intval');
    		if($pid>0){
    			$regionModel = M('region');
    			$data['status']='OK';
    			$data['area'] = $regionModel->where('pid='.$pid)->select();
    			$this->ajaxReturn($data);
    		}
    	}
    }

   public function myorder(){
 

       $this->display();
    }

    public function addGouwuche(){
        $product_id = I('get.id');
        $product_nub = I('get.nub'); 
        $gwcModel = D('gouwuche');
        $productModel = M('product');
        $data['user_id']=session('user_id');
        if ($product_id>0&&$product_nub>0) {                 
           $data['product_id'] = $product_id;
           $data['product_nub'] = $product_nub;
           if ($gwcModel->create($data)) {
              $result = $gwcModel->where("user_id={$data['user_id']} AND product_id={$product_id}")->find();
              if ($result) {
                $product_nub = $result['product_nub']+$product_nub;
                $gwcModel->product_nub=$product_nub;
                $gwcModel->where("id={$result['id']}")->save();
                // $data['staus'] = 'ok';
                // $this->ajaxReturn($data);
                $this->redirect('Gouwuche');
              }else{
                if($gwcModel->add()){             
                  $this->redirect('Gouwuche');
                }else{
                  $this->error($gwcModel->geterror());
                }
              }        
           }else{
            $this-error($gwcModel->geterror());
           }
        }
    }


    public function Gouwuche(){
     $productModel = M('product');
     $data['user_id']=session('user_id');
     $row = $productModel->join('fm_gouwuche as a ON fm_product.id = a.product_id')->where("a.user_id={$data['user_id']}")->select();
     $total=0;
     foreach ($row as $key => $value) {
       $row[$key]['total'] = $value['price']*$value['product_nub'];
       $total = $total+$row[$key]['total'];
     }
     $this->product = $row; 
     $this->total = $total;  
     $this->assign('savegouwuche',U('SaveGouwuche'));
     $this->assign('totalgouwuche',U('totalgouwuche'));
     $this->assign('addorder',U('addorder'));
     $this->display();
   }


   public function SaveGouwuche(){
    $gouwuche_id = I('post.gouwuche_id');
    $product_nub = I('post.product_nub');
    $flag = I('post.flag'); 
    $data['user_id']=session('user_id'); 
    $gwcModel = D('gouwuche');
    $data['product_id'] = $product_id;
    $data['product_nub'] = $product_nub;
    if ($gwcModel->create($data)) {  
              $result = $gwcModel->where("user_id={$data['user_id']} AND id={$gouwuche_id}")->find(); 
              if ($result) { 
                if ($flag=='you') {
                  $product_nub = $result['product_nub']+1;
                  $gwcModel->product_nub=$product_nub;
                  $gwcModel->where("id={$result['id']}")->save();
                 }else{
                  $product_nub = $result['product_nub']-1;
                   if ($product_nub<1) {
                     $gwcModel->where("id={$result['id']}")->delete();
                   }else{
                     $gwcModel->product_nub=$product_nub;
                     $gwcModel->where("id={$result['id']}")->save();
                   }
                 }
                 //  if ($flag=='you') {
                 //      $productModel=M('product');
                 //      $product = $productModel->where("id={$result['product_id']}")->find();
                 //      $product_nub = $result['product_nub']+1; 
                 //      if ($product_nub>$product['stock']) {
                 //        $back['status'] = 'false';
                 //        $back['info']='库存不足';
                 //        $this->ajaxReturn($back);
                 //      }else{
                 //        $gwcModel->product_nub=$product_nub;
                 //        $gwcModel->where("id={$result['id']}")->save();
                 //        $back['status'] = 'ok';
                 //        $back['num'] = $product_nub;
                 //        $this->ajaxReturn($back);
                 //     }
                 // }                     
                $back['status'] = 'ok';
                $back['num'] = $product_nub;
                $this->ajaxReturn($back);
             }else{
                $back['status'] = 'false';
                $back['info']=$gwcModel->geterror();
                $this->ajaxReturn($back);
             }
      }else{
               $back['status'] = 'false';
               $back['info']=$gwcModel->geterror();
               $this->ajaxReturn($back);
      }

   }

  public function totalgouwuche(){
     $product_id = I('post.product_id'); 
     $data['user_id']=session('user_id');
     $productModel = M('product');
     $data['product_id'] = $product_id;  
     $row = $productModel->join('fm_gouwuche as a ON fm_product.id = a.product_id')->where("a.user_id={$data['user_id']}")->select();
     if($row){
       $total=0;
       foreach ($row as $key => $value) {
       $row[$key]['total'] = $value['price']*$value['product_nub'];
       $total = $total+$row[$key]['total'];
       }
       $back['status'] = 'ok';
       $back['total'] = $total;
       $this->ajaxReturn($back);
    }else{
       $back['status'] = 'false';
       $back['info']=$gwcModel->geterror();
       $this->ajaxReturn($back);
    }
     $back['status'] = 'false';
     $back['info']=$gwcModel->geterror();
     $this->ajaxReturn($back);

  }

 public function addorder(){
      if (I('post.gouwuche_id')) {
         $gouwuche_id = I('post.gouwuche_id');
         $data['user_id'] = session('user_id');
         $gouwucheModel = M('gouwuche');
         $productModel = M('product');
         $product_to_orderModel = M('product_to_order');
         $total=0;

         foreach ($gouwuche_id as $key => $value) {   
             $row[]= $gouwucheModel->where("id={$value} AND user_id={$data['user_id']}")->find();
              }

         foreach ($row as $key => $value) {
            $result = $productModel ->where("id={$value['product_id']}")->find();
            $data['num'] = $value['product_nub'];
            $data['price'] = $result['price'];
            $total = $total+$data['num']*$data['price'];
         }
         $addressModel = M('address');
         $address = $addressModel->where("user_id={$data['user_id']} AND status=1")->find();    
         $region = M('region');
         $p = $region->where("id=".$address['province'])->find();
         $c = $region->where("id=".$address['city'])->find();
         $co = $region->where("id=".$address['county'])->find();
         $a = $p['name'].$c['name'].$co['name'];
         $time = time();
         $time1 = date('Ymn',$time); 
         $order_num = $time1.$time; 
         $da['user_id']= $data['user_id'];
         $da['order_num']=$order_num;
         $da['address']=$a;
         $da['total']=$total;
         $da['add_time']=date('Y-m-n H:i:s',time());
         $orderModel = M('order');
         if ($orderModel->create($da)) {
            $last_id = $orderModel->add();
            // $this->redirect('order');     
             foreach ($row as $key => $value) {
              $result = $productModel ->where("id={$value['product_id']}")->find();
              $data['product_id'] = $result['id'];
              $data['gouwuche_id'] = $value['id'];
              $data['order_id'] = $last_id;
              $data['name'] = $result['name'];
              $data['sku'] = $result['sku'];
              $data['img'] = $result['img'];
              $data['num'] = $value['product_nub'];
              $data['price'] = $result['price'];
              $data['description'] = $result['description'];
               if($product_to_orderModel->create($data)){
                   $product_to_orderModel->add();                  
              }else{
                     $this->error('加入订单出错');
               }
           }
           $this->redirect('order');
      }else{
              $this->error('加入订单出错',U('gouwuche'));
            }
    }   
}

  public function order (){
     $orderModel = M('order');
     $id = session('user_id');
     $order = $orderModel->where("user_id={$id}")->select();
     foreach ($order as $key => $value) {
       $result[]= $orderModel->join("fm_product_to_order as p ON fm_order.id=p.order_id")->where("fm_order.user_id={$id} AND fm_order.id={$value['id']}")->select();  
     }  //联查条件要写在where中      
     $this->result = $result;
     $this->assign('postorder',U('postorder'));
     $this->display();
  }

  
  public function postorder(){
    $order_id = I('get.order_id');
    $user_id = session('user_id');
    $addressModel = M('address');
    $result = $addressModel->where("user_id={$user_id}")->select();
    $region = M('region');
    foreach ($result as $key => $value) {
      $p = $region->where("id=".$value['province'])->find();
      $c = $region->where("id=".$value['city'])->find();
      $co = $region->where("id=".$value['county'])->find();
      $address[$key]=array(
                      'address' => $p['name'].$c['name'].$co['name'],
                      'status'  => $value['status'],
                      'phone'   => $value['phone'],);
    } 
    $product_to_order = M('product_to_order');
    $product = $product_to_order->where("order_id={$order_id}")->select();
    $i=0;
    foreach ($product as $key => $value) {
      $product[$key]['total'] = $value['price']*$value['num'];
      $total = $total+$value['price']*$value['num'];
      $i = $i++;
    }
    $this->i = $i;
    $this->total =$total;
    $this->product = $product;
    $this->address=$address;
    $this->assign('addaddress',U('Addaddress'));
    $this->display();
  }

}
